import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from common.functions import open_browser
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select


class Base:
    def __init__(self, browser):
        self.driver = browser
        # self.driver = open_browser(browser)
        # self.driver.maximize_window()
        # self.driver.implicitly_wait(8)

    # 获取网址
    def base_get(self, url):
        try:
            self.driver.get(url)
        except Exception as e:
            print('输入网址不正确')

    # 切换iframe
    def base_switch_iframe(self, locator):
        try:
            iframe = self.base_find_element(locator)
            self.driver.switch_to.frame(iframe)
        except Exception:
            raise Exception('切换iframe错误！')

    # 返回上一级iframe
    def base_switch_parent_iframe(self):
        try:
            self.driver.switch_to.parent_frame()
        except Exception:
            raise Exception('返回上一级iframe错误！')

    # 退出iframe
    def base_switch_default_iframe(self):
        try:
            self.driver.switch_to.default_content()
        except Exception:
            raise Exception('退出iframe错误！')

    # 查找元素
    def base_find_element(self, locator):
        try:
            return WebDriverWait(self.driver, timeout=5).until(EC.presence_of_element_located(locator))
        except Exception:
            raise Exception('元素查找错误！')

    # 查找一列元素
    def base_find_elements(self, locator):
        try:
            return WebDriverWait(self.driver, timeout=5).until(EC.presence_of_all_elements_located(locator))
        except Exception:
            raise Exception('元素查找错误！')

    # 输入
    def base_send_keys(self, locator, str):
        ele = self.base_find_element(locator)
        if ele is not None:
            ele.send_keys(str)
        else:
            print('元素不存在')

    # 获取属性
    def base_get_attribute(self, locator, str):
        ele = self.base_find_element(locator)
        if ele is not None:
            attribute = ele.get_attribute(str)
            return attribute
        else:
            print('元素不存在')

    # 获取文本
    def base_get_attribute_text(self, locator):
        ele = self.base_find_element(locator)
        if ele:
            return ele.text
        else:
            print('元素不存在')

    # 点击按钮
    def base_click(self, locator):
        ele = self.base_find_element(locator)

        if ele is not None:
            ele.click()
        else:
            print('元素不存在')

    # 前进
    def base_forward(self):
        self.driver.forward()

    # 后退
    def base_back(self):
        self.driver.back()

    # 刷新
    def base_refresh(self):
        self.driver.refresh()

    # 退出
    def base_quit(self, second=2):
        time.sleep(second)
        self.driver.quit()

    # select下拉选
    # 根据值选
    def base_select_value(self, locator, value):
        ele = self.base_find_element(locator)
        if ele is not None:
            select = Select(ele)
            return select.select_by_value(value)
        else:
            print('元素定位失败')

    # 根据文本选
    def base_select_text(self, locator, text):
        ele = self.base_find_element(locator)
        if ele is not None:
            select = Select(ele)
            return select.select_by_visible_text(text)
        else:
            print('元素定位失败')

    # 根据索引选
    def base_select_index(self, locator, index):
        ele = self.base_find_element(locator)
        if ele is not None:
            select = Select(ele)
            return select.select_by_index(index)
        else:
            print('元素定位失败')


if __name__ == '__main__':
    browser = open_browser('chrome')
